﻿using System;
using System.IO;
using System.Linq;

namespace ClearAllSubDirectoryAndFile
{
    internal class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("输入exit退出程序");
                Console.WriteLine("输入clear清空屏幕");
                Console.WriteLine("[父文件夹路径] [需要删除的文件夹名]... [需要删除的文件名]...");

                var command = Console.ReadLine();
                if (string.IsNullOrEmpty(command))
                {
                    Console.WriteLine("空命令，无法识别");
                    continue;
                }
                else if ("exit".Equals(command.ToLower())) return;
                else if ("clear".Equals(command.ToLower()))
                {
                    Console.Clear();
                    continue;
                }
                else
                {
                    var ps = command.Split(' ');
                    if (ps.Length < 2)
                    {
                        Console.WriteLine("名称错误，参数不足");
                        continue;
                    }
                    else if (!Directory.Exists(ps[0]))
                    {
                        Console.WriteLine("父文件夹不存在");
                        continue;
                    }
                    else
                    {
                        DirectoryInfo directoryInfo = new DirectoryInfo(ps[0]);
                        Delete(directoryInfo, ps.Skip(1).Select(p => p.ToLower()).ToArray());
                    }
                }
            }
        }
        static void Delete(DirectoryInfo directoryInfo, params string[] names)
        {
            if (directoryInfo == null || names == null || names.Length <= 0) return;
            var subf = directoryInfo.GetFiles();
            if (subf.Length > 0)
            {
                foreach (var f in subf)
                {
                    if (names.Contains(f.Name.ToLower()))
                    {
                        f.Delete();
                        Console.WriteLine(string.Concat("删除文件：", f.FullName));
                    }
                }
            }
            var subd = directoryInfo.GetDirectories();
            if (subd.Length > 0)
            {
                foreach (var d in subd)
                {
                    if (names.Contains(d.Name.ToLower()))
                    {
                        d.Delete(true);
                        Console.WriteLine(string.Concat("删除文件夹：", d.FullName));
                    }
                    else Delete(d, names);
                }
            }
        }
    }
}
